package hamsterteam.hop.ejb;

import hamsterteam.hop.model.Etudiant;
import hamsterteam.hop.model.Projet;
import java.util.List;
import javax.persistence.*;
import javax.ejb.Stateless;

@Stateless
public class EtudiantEJB {

    // ======================================
    // =             Attributes             =
    // ======================================
    @PersistenceContext(unitName = "hopPU")
    private EntityManager em;

    // ======================================
    // =           Public Methods           =
    // ======================================
    public List<Etudiant> findAll() {
        Query query = em.createNamedQuery("Etudiant.findAll");
        return query.getResultList();
    }
    
    public Etudiant findId(long selectedId) {
        Query query = em.createQuery("SELECT p FROM Etudiant p WHERE p.id = :idP");
        query.setParameter("idP", selectedId);
        return (Etudiant) query.getSingleResult();
    }

    public Etudiant create(Etudiant etu) {
        em.persist(etu);
        return etu;
    }

    public Etudiant update(Etudiant etu) {
        return em.merge(etu);
    }

    public void delete(List<Etudiant> list) {
        for (Etudiant etu : list) {
            delete(etu);
        }
    }

    public void delete(Etudiant etu) {
        em.remove(em.merge(etu));
    }
}
